/*
 * @Author: lclangcheng
 * @Date: 2018-11-01 14:26:20
 * @Last Modified by: lclangcheng
 * @Last Modified time: 2019-12-27 11:00:56
 */

'use strict'

const log = require('../libs/logger')
const BaseComponent = require('./base_component')
const { SearchService } = require('../service')
const { App } = require('../config//constant')
const config = require('config')

class Search extends BaseComponent {
  constructor () {
    super()
    this.name = 'Search'
    this.search = this.search.bind(this)
    this.complete = this.complete.bind(this)
    this.hotword = this.hotword.bind(this)
    this.filterKeyword = this.filterKeyword.bind(this)
  }

  /**
   *
   * 获取列表
   * @param {Object} ctx
   * @param {Function} next
   */
  async search (ctx, next) {
    log.handleLogger(ctx, this.name, this.search.name)
    const query = ctx.request.query
    try {
      const res = await SearchService.doSerach({ keyword: query.keyword, page: query.page, page_size: query.page_size })
      ctx.body = { status: 200, data: res }

      const options = {
        platform: query.platform || '',
        keyword: query.keyword,
        res_total: res.total,
        referer: ctx.request.headers['referer'] || '',
        user_agent: ctx.request.headers['user-agent'] || '',
        optime: Math.ceil(Date.now().valueOf() / 1000),
        app: App,
        ip: ctx.request.ip && ctx.request.ip.replace(/::ffff:/, '')
      }
      SearchService.emit('', config.kMyMqSearchTopic, options)
      next && await next()
    } catch (err) {
      log.errHandleLogger(ctx, this.name, this.search.name, err)
      const result = {
        status: err.status || 500,
        message: err.status ? err.message : '服务器内部错误'
      }
      ctx.body = result
    }
  }

  /**
   *
   * 过滤敏感词
   * @param {Object} ctx
   * @param {Function} next
   */
  async filterKeyword (ctx, next) {
    log.handleLogger(ctx, this.name, this.filterKeyword.name)
    const query = ctx.request.query
    try {
      const keyword = await SearchService.filterKeyword(query.keyword)
      if (!keyword) {
        ctx.body = { status: 200, data: { list: [] } }
        return
      }
      next && await next()
    } catch (err) {
      log.errHandleLogger(ctx, this.name, this.filterKeyword.name, err)
      const result = {
        status: err.status || 500,
        message: err.status ? err.message : '服务器内部错误'
      }
      ctx.body = result
    }
  }

  /**
   *
   * 获取列表
   * @param {Object} ctx
   * @param {Function} next
   */
  async complete (ctx, next) {
    log.handleLogger(ctx, this.name, this.complete.name)
    const query = ctx.request.query
    try {
      const res = await SearchService.complete(query)
      ctx.body = { status: 200, data: res }
      next && await next()
    } catch (err) {
      log.errHandleLogger(ctx, this.name, this.complete.name, err)
      const result = {
        status: err.status || 500,
        message: err.status ? err.message : '服务器内部错误'
      }
      ctx.body = result
    }
  }

  /**
   *
   * 获取热词
   * @param {Object} ctx
   * @param {Function} next
   */
  async hotword (ctx, next) {
    log.handleLogger(ctx, this.name, this.hotword.name)
    // const query = ctx.request.query
    try {
      const res = await SearchService.hotword()
      ctx.body = { status: 200, data: res }
      next && await next()
    } catch (err) {
      log.errHandleLogger(ctx, this.name, this.hotword.name, err)
      const result = {
        status: err.status || 500,
        message: err.status ? err.message : '服务器内部错误'
      }
      ctx.body = result
    }
  }
}

module.exports = new Search()
